/**
 * 加载渲染表单
 * @param type
 */
function renderForm(type) {
    layui.use("form", function () {
        var form = layui.form;
        form.render(type);
    });
}

/**
 * 加载渲染select表单
 */
function renderSelect() {
    var type = 'select';
    renderForm(type);
}

/**
 * 加载商品下拉框
 * @param id
 * @param data
 * @param goodId
 * @returns {string}
 */
function loadGoodSelect(id, data, goodCode) {
    var appenddata = "";
    appenddata += "<select id=select_" + id + " lay-filter=\'sel-goods-filter\'>\n";
    appenddata += ("<option value=" + "\' \'" + ">" + "请选择" + "</option> \n");
    data.forEach(function (value, index, array) {
        appenddata += ('<option value=' + value.goodCode + (goodCode == value.goodCode ? ' selected' : ' ') + '>' + value.goodName + '</option>\n');
    });
    appenddata += "</select>";
    return appenddata;
}

var alloption = '<option value=\'\' selected>全部</option>';

/**
 * 同步加载仓库下拉框
 * @param id
 * @returns {Promise<*>}
 */
async function initStorSelect(id) {
    var res = await new Promise(function (resolve, reject) {
        layui.use(["form", "jquery"], function () {
            var form = layui.form, $ = layui.jquery;
            $.ajax({
                type: "GET",
                url: "/base/getStorList",
                async: false,
                success: function (result) {
                    var selection = $('#' + id);
                    selection.find("option").remove();
                    selection.append(alloption);
                    result.data.forEach(function (currval, index, array) {
                        var option = '<option value=' + currval.storCode + '>' + currval.abbrName + '</option>'
                        selection.append(option);
                    });
                    form.render('select', 'select-condition');
                    resolve("done");
                },
                error: function (data) {
                    layer.msg("初始化仓库select失败,data:" + data.toString());
                    reject(data);
                }
            });
        });
    }).then(function (result) {
        console.log("初始化仓库完成", result);
        return result;
    });

    return res;
}

/**
 * sync初始供货商信息
 * @param id
 * @returns {Promise<*>}
 */
async function initSuppInfoSelect(id) {
    var res = await new Promise(function (resolve, reject) {
        layui.use(["form", "jquery"], function () {
            var form = layui.form, $ = layui.jquery;

            $.ajax({
                type: "GET",
                url: "/base/getSuppInfoList",
                async: false,
                success: function (result) {
                    var selection = $('#' + id);
                    selection.find("option").remove();
                    selection.append(alloption);
                    result.data.forEach(function (currval, index, array) {
                        var option = '<option value=' + currval.suppCode + '>' + currval.abbrName + '</option>'
                        selection.append(option);
                    });
                    form.render('select', 'select-condition');
                    resolve("done");
                },
                error: function (data) {
                    layer.msg("初始化仓库select失败,data:" + data.toString());
                    reject(new Error(data));
                }
            });
        });
    }).then(function (result) {
        console.log("初始化供货商完成", result);
        return result;
    });

    return res;
}

/**
 * 同步初始化系统参数
 * @param id
 * @param url
 * @param params
 * @returns {Promise<*>}
 */
async function initCodeTableSelect(id, url, params) {
    var res = await new Promise(function (resolve, reject) {
        layui.use(["form", "jquery"], function () {
            var form = layui.form, $ = layui.jquery;
            $.ajax({
                type: "GET",
                url: url,
                data: params,
                async: false,
                success: function (result) {
                    var selection = $('#' + id);
                    selection.find("option").remove();
                    selection.append(alloption);
                    result.data.forEach(function (currval, index, array) {
                        var option = '';
                        if (params != 'undefined' && null != params && params.code == currval.sysValue) {
                            option = '<option value=' + currval.sysValue + ' selected>' + currval.codeDesc + '</option>';
                        } else {
                            option = '<option value=' + currval.sysValue + '>' + currval.codeDesc + '</option>';
                        }
                        selection.append(option);
                    });
                    form.render('select');
                    resolve("done");
                },
                error: function (data) {
                    layer.msg("初始化select失败, select id:" + id.toString());
                    reject(data.toString());
                }
            });
        });
    }).then(function (result) {
        console.log("渲染系统参数组件", id, result);
        return result;
    });

    return res;
}

/**
 * 同步初始化系统参数
 * @param id
 * @param url
 * @param params
 * @returns {Promise<*>}
 */
function loadSourceSelect(id, data, source) {
        var appenddata = "";
        appenddata += "<select id=source_" + id + " lay-event=\'goodSourceChange\'>\n";
        appenddata += ("<option value=" + "\' \'" + ">" + "请选择" + "</option> \n");
        data.forEach(function (value, index, array) {
            appenddata += ('<option value=' + value.source + (source == value.source ? ' selected' : ' ') + '>' + value.sourceName + '</option>\n');
        });
        appenddata += "</select>";
        return appenddata;
}

/**
 * 获取客户信息
 * @param id
 * @param url
 * @param params
 * @returns {Promise<*>}
 */
async function initCustInfoSelect(id) {
    var res = await new Promise(function (resolve, reject) {
        layui.use(["form", "jquery"], function () {
            var form = layui.form, $ = layui.jquery;

            $.ajax({
                type: "POST",
                url: "/base/getAllCustInfo",
                async: false,
                success: function (result) {
                    var selection = $('#' + id);
                    selection.find("option").remove();
                    selection.append(alloption);
                    result.data.forEach(function (currval, index, array) {
                        var option = '<option value=' + currval.custCode + '>' + currval.custName + '</option>';
                        selection.append(option);
                    });
                    form.render('select');
                    resolve("done");
                },
                error: function (data) {
                    layer.msg("初始化select失败, select id:" + id.toString());
                    reject(data.toString());
                }
            });
        });
    }).then(function (result) {
        console.log("渲染系统参数组件", id, result);
        return result;
    });

    return res;
}

/**
 * 获取客户联系人信息
 * @param id
 * @param url
 * @param params
 * @returns {Promise<*>}
 */
async function selectClientCont(contInfo,custCode) {

    var res = await new Promise(function (resolve, reject) {
        layui.use(["form", "jquery"], function () {
            var form = layui.form, $ = layui.jquery;

            $.ajax({
                type: "POST",
                url: "/base/findContInfo",
                async: false,
                data: {"custCode": custCode},
                success: function (result) {
                    var selection = $('#' + contInfo);
                    selection.find("option").remove();
                    result.data.custLinkData.forEach(function (currval, index, array) {
                        var option = '<option value=' + currval.id + '>' + currval.contName + '</option>';
                        selection.append(option);
                    });
                    $('#cont_phone').val(result.data.custLinkData[0].contPhone);
                    $('#cont_code').val(result.data.custLinkData[0].id);
                    $('#address').val(result.data.address);
                    form.render('select');
                    resolve("done");
                },
                error: function (data) {
                    layer.msg("初始化select失败, select id:" + contInfo.toString());
                    reject(data.toString());
                }
            });
        });
    }).then(function (result) {
        console.log("渲染系统参数组件", contInfo, result);
        return result;
    });

    return res;
}

/**
 * 同步加载角色下拉框
 * @param id
 * @param url
 * @param params
 * @returns {Promise<*>}
 */
async function initRoleSelection(id, url, params) {
    var res = await new Promise(function (resolve, reject) {
        layui.use(["form", "jquery"], function () {
            var form = layui.form, $ = layui.jquery;

            $.ajax({
                type: "GET",
                url: url,
                data: params,
                async: false,
                success: function (result) {
                    var selection = $('#' + id);
                    selection.find("option").remove();
                    selection.append(alloption);
                    result.data.forEach(function (currval, index, array) {
                        var option = '<option value=' + currval.id + '>' + currval.roleName + '</option>'
                        selection.append(option);
                    });
                    form.render('select', 'select-condition');
                    resolve("done");
                },
                error: function (data) {
                    layer.msg("初始化select失败, select id:" + id.toString());
                    reject(new Error(data.message));
                }
            });
        });
    }).then(function (result) {
        console.log("加载角色下拉框完成", result);
        return result;
    });

    return res;
}

/**
 * 同步加载机构下拉框
 * @param id
 * @param url
 * @param params
 * @returns {Promise<*>}
 */
async function initOrganSelect(id, url, params) {
    var res = await new Promise(function (resolve, reject) {
        layui.use(["form", "jquery"], function () {
            var form = layui.form, $ = layui.jquery;

            $.ajax({
                type: "GET",
                url: url,
                data: params,
                async: false,
                success: function (result) {
                    var selection = $('#' + id);
                    selection.find("option").remove();
                    selection.append(alloption);
                    result.data.forEach(function (currval, index, array) {
                        var option = '<option value=' + currval.id + '>' + currval.organName + '</option>'
                        selection.append(option);
                    });
                    form.render('select');
                    resolve("done");
                },
                error: function (data) {
                    layer.msg("初始化select失败, select id:" + id.toString());
                    reject(new Error(data.message));
                }
            });
        });
    }).then(function (result) {
        console.log("加载机构下拉框完成", JSON.stringify(result));
        return result;
    });

    return res;
}

async function initOrganSelect(id, params){
    var res = await new Promise(function (resolve, reject) {
        layui.use(["form", "jquery"], function () {
            var form = layui.form, $ = layui.jquery;

            $.ajax({
                type: "GET",
                url: '/sys/getOrganList',
                data: params,
                async: false,
                success: function (result) {
                    var selection = $('#' + id);
                    selection.find("option").remove();
                    selection.append(alloption);
                    result.data.forEach(function (currval, index, array) {
                        var option = '<option value=' + currval.id + '>' + currval.organName + '</option>'
                        selection.append(option);
                    });
                    form.render('select');
                    resolve("done");
                },
                error: function (data) {
                    layer.msg("初始化select失败, select id:" + id.toString());
                    reject(new Error(data.message));
                }
            });
        });
    }).then(function (result) {
        console.log("加载机构下拉框完成", JSON.stringify(result));
        return result;
    });

    return res;
}